import logging
import os
import time

from config.config import LOGS_PATH

# 日志目录
log_dir = LOGS_PATH

# 日志文件名
log_file = os.path.join(log_dir, f"app_test_{time.strftime('%Y%m%d')}.log")

# 创建 logger
logger: logging.Logger = logging.getLogger("AppTestLogger")
logger.setLevel(logging.DEBUG)

# 文件 Handler
fh = logging.FileHandler(log_file, encoding="utf-8")
fh.setLevel(logging.DEBUG)

# 控制台 Handler
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

# 日志格式
formatter = logging.Formatter(
    fmt="%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S"
)
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 避免重复添加 handler
if not logger.handlers:
    logger.addHandler(fh)
    logger.addHandler(ch)
